<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class EvalController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {   
        //保存搜索条件
        // dd($request);
        $where = [];
        //实例化要操作的表
        $db = DB::table('product_evaluation');
        
        $list = DB::table('product_evaluation')
            ->join('evaluation_reply', 'product_evaluation.order_id', '=', 'evaluation_reply.evaluation_id')
            ->select('product_evaluation.*', 'evaluation_reply.reply_content', 'evaluation_reply.reply_time');
        if($request->has('title')){
            // 获取要搜索的title字段的值
            $title = $request->input('title');
            //添加where模糊匹配条件
            $list->where('title','like',"%{$title}%");
            $where['title'] = $title;
        }
        // 分页
        $list = $list->paginate(3);   
        // dd($list);
        return view('admin.eval',['list'=>$list, 'where'=>$where]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $list = DB::table('product_evaluation')->where('order_id',$id)->first();
        return view('eval.edit',['list'=>$list,'id'=>$id]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $data = $request->only('reply_content');
        $row = DB::table('evaluation_reply')->where('evaluation_id',$id)->update($data);
        if($row>0){
            return redirect('admin/eval');
        }else{
            return '修改失败';
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //

        $row = DB::table('product_evaluation')->where('id',$id)->delete();
        $rows = DB::table('evaluation_reply')->where('id',$id)->delete();
        if($row>0 && $rows>0){
            return redirect('admin/eval');
        }
    }
}
